﻿package _5_ProgressBarAndSlider;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
 * 2011-6-5 22:09:18
 * @author Administrator
 * 用滑块设置颜色的RGB值
 */
public class TJSlider {
    JFrame frame;
    Container contentPane;
    JPanel colorPane;
    int rgb;
    JPanel sliderPane;
    JSlider rSlider,gSlider,bSlider;
    public TJSlider(){
        frame=new JFrame("滑块测试");
        contentPane=frame.getContentPane();
        rgb=0x000000;
        colorPane=new JPanel(){
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public void paint(Graphics g){
                g.setColor(new Color(rgb));
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
            }
        };
        colorPane.setPreferredSize(new Dimension(500,500));
        contentPane.add(colorPane,BorderLayout.CENTER);
        sliderPane=new JPanel();
        sliderPane.setLayout(new GridLayout(3,1));
        rSlider=new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
        rSlider.setPaintLabels(true);
        rSlider.setPaintTicks(true);
        rSlider.setPaintTrack(true);
        rSlider.setMajorTickSpacing(25);
        rSlider.setMinorTickSpacing(5);
        rSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value=rSlider.getValue();
                rgb=(rgb&0xffff);
                rgb+=value<<16;
                colorPane.repaint();
            }
        });
        sliderPane.add(rSlider);
        gSlider=new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
        gSlider.setPaintLabels(true);
        gSlider.setPaintTicks(true);
        gSlider.setPaintTrack(true);
        gSlider.setMajorTickSpacing(25);
        gSlider.setMinorTickSpacing(5);
        gSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value=gSlider.getValue();
                rgb=(rgb&0xff00ff);
                rgb+=value<<8;
                colorPane.repaint();
            }
        });
        sliderPane.add(gSlider);
        bSlider=new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
        bSlider.setPaintLabels(true);
        bSlider.setPaintTicks(true);
        bSlider.setPaintTrack(true);
        bSlider.setMajorTickSpacing(25);
        bSlider.setMinorTickSpacing(5);
        bSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value=bSlider.getValue();
                rgb=(rgb&0xffff00);
                rgb+=value;
                colorPane.repaint();
            }
        });
        sliderPane.add(bSlider);
        contentPane.add(sliderPane,BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        new TJSlider();
    }
}
